/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: tiny_array.h
 *
 * Purpose: dynamic array container implementation
 *
 * Developer:
 *   wen.gu , 2017-03-27
 *
 * TODO: 
 *
 ***************************************************************************/

#ifndef __TINY_ARRAY_H__
#define __TINY_ARRAY_H__


#include "general_type.h"


#ifdef __cplusplus
extern "C" {
#endif

/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/
/**the Minimum default count of array element */
#define DEFAULT_ARRAY_CAPACITY 8
/***************************************************************************
 *
 * data structure declaration
 *
 ***************************************************************************/
/**@ brief array element free callback function */
typedef void (*tfArrayElemFreeFunc_t)(void *elem); 

/***************************************************************************
 *
 * API declaration
 *
 ***************************************************************************/

/**
 *@brief  create dynamic array instance.
 *
 *@param  length    [in] the default count of array element.
 *@param  elem_free [in] the callback function for free array element.
 *
 *@return success: array instance, fail: NULL.
 *@see
 *
 */
G_API GPHD tfArrayCreate(GU32 length, tfArrayElemFreeFunc_t elem_free);

/**
 *@brief  destroy dynamic array instance.
 *
 *@param  array_hd  [in] the handle of array instance.
 *
 *@return none.
 *@see
 *
 */
G_API void tfArrayDestroy(GPHD array_hd);

/**
 *@brief  get element value from dynamic array .
 *
 *@param  array_hd  [in] the handle of array instance.
 *@param  index     [in] the index of array element.
 *@param  value     [io] the value of element.
 *
 *@return success: G_OK, fail: error code.
 *@see
 *
 */
G_API GS32 tfArrayGet(GPHD array_hd, GU32 index, void **value);

/**
 *@brief  set element value to dynamic array .
 *
 *@param  array_hd  [in] the handle of array instance.
 *@param  index     [in] the index of array element.
 *@param  value     [in] the value of element.
 *
 *@return success: G_OK, fail: error code.
 *@see
 *
 */
G_API GS32 tfArraySet(GPHD array_hd, GU32 index, void *value);

/**
 *@brief  append a element to the tail of dynamic array.
 *
 *@param  array_hd  [in] the handle of array instance.
 *@param  value     [in] the value of element.
 *
 *@return success: G_OK, fail: error code.
 *@see
 *
 */
G_API GS32 tfArrayAppend(GPHD array_hd, void *value);

/**
 *@brief  remove element from dynamic array .
 *
 *@param  array_hd  [in] the handle of array instance.
 *@param  index     [in] the index of array element.
 *
 *@return success: G_OK, fail: error code.
 *@see
 *
 */
G_API GS32 tfArrayRemove(GPHD array_hd, GU32 index);

/**
 *@brief  check is array empty.
 *
 *@param  array_hd  [in] the handle of array instance.
 *
 *@return empty: GTRUE, not: GFALSE.
 *@see
 *
 */
G_API GBOL tfArrayEmpty(GPHD array_hd);

/**
 *@brief  how many element in current array.
 *
 *@param  array_hd  [in] the handle of array instance.
 *
 *@return success: the count of element, fail: 0.
 *@see
 *
 */
G_API GU32 tfArrayElementCount(GPHD array_hd);




#ifdef __cplusplus
}
#endif

#endif //end of __TINY_ARRAY_H__
